﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework.Audio;
using System.IO;

namespace VocabManager.Voice
{
    public class SoundManager
    {
        private Microphone _currentMicrophone;
        private MemoryStream _currentRecordingStream;
        private byte[] _audioBuffer;
        private bool _stopRequested;
        private int _sampleRate = 0;
        string _fileName;
        public bool IsRecording
        {
            get;
            set;
        }
        public SoundManager(string fileName)
        {
            _fileName = fileName;
        }
        public string FileName
        {
            get
            {
                return _fileName;
            }
        }

        public SoundManager():this(DateTime.Now.ToFileTimeUtc()+".wav")
        {

        }
        public void StartRecording()
        {
            if (_currentMicrophone == null)
            {
                _currentMicrophone = Microphone.Default;
                _currentMicrophone.BufferReady += new EventHandler<EventArgs>(_currentMicrophone_BufferReady);
                _audioBuffer = new byte[_currentMicrophone.GetSampleSizeInBytes(_currentMicrophone.BufferDuration)];
                _sampleRate = _currentMicrophone.SampleRate;
            }

                 
            _stopRequested = false;
            _currentRecordingStream = new MemoryStream(1048576);
            IsRecording = true;
            _currentMicrophone.Start();
        }

       
        public void RequestStopRecording()
        {
            _stopRequested = true;
        }
        void _currentMicrophone_BufferReady(object sender, EventArgs e)
        {
            _currentMicrophone.GetData(_audioBuffer);
            _currentRecordingStream.Write(_audioBuffer, 0, _audioBuffer.Length);
            if (!_stopRequested) return;
            _currentMicrophone.Stop();
            var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
            if (isoStore.FileExists(_fileName))
                isoStore.DeleteFile(_fileName);
            using (var targetFile = isoStore.CreateFile( _fileName))
            {
                WaveHeaderWriter.WriteHeader(targetFile, (int)_currentRecordingStream.Length, 1, _sampleRate);
                var dataBuffer = _currentRecordingStream.GetBuffer();
                targetFile.Write(dataBuffer, 0, (int)_currentRecordingStream.Length);
                targetFile.Flush();
                targetFile.Close();
            }
            _currentMicrophone.BufferReady -= new EventHandler<EventArgs>(_currentMicrophone_BufferReady);
         
            IsRecording = false;
            _stopRequested = false;
       
        }
        private SoundEffectInstance _currentSound = null;
        public void PlayRecording()
        {
            SoundEffect se;
            if (_currentSound != null)
            {
                _currentSound.Stop();
                _currentSound = null;
            }
            var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
            if (isoStore.FileExists(_fileName))
            {
                using (var fileStream = isoStore.OpenFile(_fileName, FileMode.Open))
                {
                    se = SoundEffect.FromStream(fileStream);
                    fileStream.Close();//not really needed, but it makes me feel better. 
                }

                _currentSound = se.CreateInstance();
                _currentSound.Play();
            }
        }


    }
}
